//
//  CenterViewController.m
//  Eath
//
//  Created by yuhaya on 14-8-23.
//  Copyright (c) 2014年 yuhaya. All rights reserved.
//

#import "CenterViewController.h"
#import "Tool.h"
#import "GenearchListTableViewController.h"
#import "DKScrollingTabController.h"
#import "BuyViewController.h"
#import "StudentListTableViewController.h"
#import "CenterViewCell.h"
#import "LoginViewController.h"
#import "User.h"
#import "GenearchAddViewController.h"
#import "StudentList.h"

@interface CenterViewController ()
@end

static NSString * cellId = @"center_cell_id";

@implementation CenterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    NSDictionary * dict = [User loginInfo];
//    NSLog(@"%@",dict);
    self.name = dict[@"username"];
    self.phone = dict[@"phone"];
    self.sex = [dict[@"sex"] integerValue];
    
    [self.tableView reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    if(![User isLogin]){
//        
//        LoginViewController * login = [[LoginViewController alloc] init];
//        [self.navigationController pushViewController:login animated:YES];
//    
//        self.name = @"匿名用户";
//        self.phone = @"未知";
//        self.sex = 0;
//        
//    }
    
    self.photo = @"default_head";
    
    self.title = @"设置";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick)];
    
    UINib *nib = [UINib nibWithNibName:@"CenterViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:cellId];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UITableViewCell * cell = [[UITableViewCell alloc] init];
        UIImageView * img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.photo]];
        img.frame = CGRectMake(10, 15, 50, 50);
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(80, 15, 80, 20)];
        name.font = [UIFont boldSystemFontOfSize:15];
        name.text = self.name;
        UILabel * ideText = [[UILabel alloc] initWithFrame:CGRectMake(80, 50, 80, 20)];
        ideText.text = @"性别";
        ideText.font = [UIFont systemFontOfSize:13];
        UILabel * phone = [[UILabel alloc] initWithFrame:CGRectMake(180, 15, 100, 20)];
        phone.text = self.phone;
        phone.font = [UIFont italicSystemFontOfSize:13];
        UILabel * type = [[UILabel alloc] initWithFrame:CGRectMake(180, 50, 100, 20)];
        type.text = self.sex ? @"女" : @"男";
        type.font = [UIFont systemFontOfSize:13];
        
        [cell.contentView addSubview:img];
        [cell.contentView addSubview:name];
        [cell.contentView addSubview:ideText];
        [cell.contentView addSubview:phone];
        [cell.contentView addSubview:type];
        
        return cell;
    }else{
    
        CenterViewCell *  cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
        
        switch (indexPath.section) {
            case 1:
                cell.img.image = [UIImage imageNamed:@"buy"];
                cell.item.text = @"购买小叮当";
                break;
                
            case 2:
                cell.img.image = [UIImage imageNamed:@"student"];
                cell.item.text = @"学生信息设置";
                break;
                
            case 3:
                cell.img.image = [UIImage imageNamed:@"sprelation"];
                cell.item.text = @"关系信息设置";
                break;
                
            case 4:
                cell.img.image = [UIImage imageNamed:@"parents"];
                cell.item.text = @"家长信息设置";
                break;
                
            case 5:
                cell.img.image = [UIImage imageNamed:@"clock"];
                cell.item.text = @"提醒";
                break;
                
            case 6:
                cell.img.image = [UIImage imageNamed:@"update"];
                cell.item.text = @"升级";
                break;
                
            default:
                break;
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        GenearchAddViewController * genEdit = [[GenearchAddViewController alloc] init];
        genEdit.purpose = 1;
        genEdit.gen = [User loginGen];
        [self.navigationController pushViewController:genEdit animated:YES];
        
    }else if (indexPath.section == 1){
        
        BuyViewController * buyController = [[BuyViewController alloc] init];
        [self.navigationController pushViewController:buyController animated:YES];
        
    }else if (indexPath.section == 2){
        
        StudentListTableViewController * stu_list = [[StudentListTableViewController alloc] init];
//        stu_list.stuList = [StudentList shareStudentList];
//        stu_list.stuList.detegate = stu_list;
//        stu_list.studentListData = [stu_list.stuList getStudentList];
        stu_list.operate = 2;
        [self.navigationController pushViewController:stu_list animated:YES];
        
    }else if (indexPath.section == 3){
        
        GenearchListTableViewController * genList = [[GenearchListTableViewController alloc] initWithStyle:UITableViewStylePlain];
        genList.purpose = 4;
        
//        StudentListTableViewController * stu_list = [[StudentListTableViewController alloc] init];
//        stu_list.operate = 4;
//        [self.navigationController pushViewController:stu_list animated:YES];
        [self.navigationController pushViewController:genList animated:YES];
        
    }else if(indexPath.section == 4){
        
        GenearchListTableViewController * subGenList = [[GenearchListTableViewController alloc] initWithStyle:UITableViewStylePlain];
        subGenList.purpose = 1;
        [self.navigationController pushViewController:subGenList animated:YES];
        
    }else if (indexPath.section == 5){
        
        StudentListTableViewController * stu_list = [[StudentListTableViewController alloc] init];
        stu_list.operate = 3;
        [self.navigationController pushViewController:stu_list animated:YES];
        
    }else if (indexPath.section == 6){
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 80;
    }else{
        return 40;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 7;
}
- (void) rightClick
{
    [self.drawerController toggleDrawerSide:XHDrawerSideRight animated:YES completion:^(BOOL finished){
        
    }];
}

@end
